<template>
	<view class="container">
		<view>NFC测试</view>
		<button @click="handeleReadNFC">读取NFC</button>
		<button @click="handeleCloure">销毁NFC</button>
		<button @click="handeleWrite">写入数据NFC</button>
		
	</view>
</template>

<script>
	import NFC from './NFC.js'
	export default {
		data() {
			return {

			}
		},
		mounted() {
			this.handleCustomEventBind = this.handleCustomEvent.bind(this);
			uni.$on('customEvent', this.handleCustomEventBind);
		},
		onLoad() {
			NFC.listenNFCStatus()
			
		},
		onShow() {
			// this.handeleReadNFC()
			
				// uni.showLoading({
				// 	title: '读取NFC中...'
				// })
			
		},
		methods: {
			handleCustomEvent(event) {
				
				uni.hideLoading()
				console.log('获取到NFC数据：', event.data);
				// 使用正则表达式匹配 id 和 name 的值
				// const regex = /id:(\d+),name:(\d+)/;
				// const matches = event.data.match(regex);
				
				// if (matches) {
				//   const id = matches[1];   // 获取 id 的值
				//   const name = matches[2]; // 获取 name 的值
				//   let obj = {}
				//   obj.id = id
				//   obj.name = name
				//   this.handleNavigateTo(obj)
				// } else {
				//   console.log("未找到匹配的值");
				// }
				
			},
			handleNavigateTo(obj){
				uni.hideLoading()
				uni.navigateTo({
					url:'/pages/work/common/RFID?obj='+JSON.stringify(obj)
				})
			},
			
			
			// 读取
			handeleReadNFC() {
				 NFC.readData();
			},
			// 写入
			handeleWrite() {
				uni.hideLoading()
				uni.showModal({
					title: "温馨提示",
					content: "此操作将抹除芯片中的数据，且不可逆",
					success: function(res) {
						if (res.confirm) {
							let arr = '87'
							NFC.writeData(arr);
						}
					}
				})
			},
			// 销毁
			handeleCloure() {
				NFC.nfcRuning()
			},
			// 读取RFID
			handeleRFID() {
			}
		},
			
		onUnload(){
			uni.hideLoading()
		}
	}
</script>

<style scoped>
	.container {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
</style>